'use strict';

var app = angular.module('app');
app.controller('updatePersonHouseController', ['$scope','$rootScope','$state','$timeout','toaster','$modal',
	function($scope, $rootScope, $state, $timeout, toaster, $modal) {
		
		$scope.record = {
			relationType:'0',
			objcetType:'0'
		};
		
		if($state.includes('**.personHouse.update')) {
			$scope.isUpdate = true;
			$scope.reset = false;
			$scope.title = "修改人员房屋关系";
			var id = $state.params.id;
			activate(id);
		} else if($state.includes('**.personHouse.create')) {
			$scope.isAdd = true;
			$scope.title = "新增人员房屋关系";
			$scope.reset = true;
		}
		validate();
		$scope.resetFrom = function(){
			$scope.record = {
				relationType:'0',
				objcetType:'0'
			};
		}
		$scope.selUser = function() {
			$scope.record.personId = $scope.record.person.split('_')[0];
			$scope.record.cardNo = $scope.record.person.split('_')[1];
		}
		
		$scope.submit = function() {
			var m = $scope.record;
			if(m) {
				$scope.record.status = '0';
				$scope.isDisabled = true; // 提交disabled
				$.ajax({
					type: 'POST',
					dataType: 'json',
					contentType: 'application/json;charset=UTF-8',
					url: 'dac/userManager/personHouse/update',
					data: angular.toJson($scope.record)
				}).then(function(result){
					if(result.httpCode == 200) { // 成功
					toaster.clear('*');
					toaster.pop('success', '', "保存成功");
					$timeout(function() {
						$state.go('main.member.personHouse.list');
					}, 2000);
				} else {

					toaster.clear('*');
					toaster.pop('error', '', result.msg);
					$scope.isDisabled = false;
				}
				});
			}
		}
		
		// 初始化页面
		function activate(id) {
			$scope.loading = true;
			$.ajax({
				type: 'POST',
				dataType: 'json',
				contentType: 'application/json;charset=UTF-8',
				url: 'dac/userManager/personHouse/detail',
				data: angular.toJson({
					'id': id
				})
			}).then(function(result) {
				$scope.loading = false;
				if(result.httpCode == 200) {
					$scope.record = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}

		// 表单验证
		function validate() {
			jQuery('form').validate({
				debugger: true,
				rules: {
					personName: {
						required: true,
						stringCheck: [],
						maxLengthB: [20]
					},
					personType: {
						required: true
					},
					relationType: {
						required: true
					},
					mobileNo: {
						required: true,
						stringCheck: [],
						maxLengthB: [11]
					},
					address: {
						required: true,
						maxLengthB: [200]
					},
					startDate: {
						required: true
					},
					endDate: {
						required: true
					}
				},
				messages: {
					personName: {
						required: '请填写人员姓名',
						maxLengthB: "姓名不得超过{0}个字符"
					},
					personType: {
						required: '请选择人员类别'
					},
					relationType: {
						required: '请选择关系类别'
					},
					mobileNo: {
						required: '请填写您的移动电话',
						maxLengthB: "身份证号码不得超过{0}个字符"
					},
					address: {
						required: '请填写居住地址',
						maxLengthB: "居住地址不得超过{0}个字符"
					},
					startDate: {
						required: '请选择开始时间'
					},
					endDate: {
						required: '请选择结束时间'
					}
				},
				submitHandler: function() {
					$scope.submit();
				}
			});
		}
		$scope.openInfo = function() {
			var objcetType = $scope.record.objcetType;
			if("0" == objcetType) {
				$scope.openHouseInfo();
			} else if("1" == objcetType) {
				$scope.openBuildingInfo();
			}
		}
		//选择房屋类型时清空房屋地址，重新选择
		$scope.rewriteInfo = function() {
			$scope.record.address = "";
		};
		//房屋模态框
		$scope.openHouseInfo = function() {
			var modalInstance = $modal.open({
				templateUrl: 'personHouseList.html', // script标签中定义的id
				controller: 'personHouseModalCtrl', // modal对应的Controller
				size: 'lg',
				resolve: {
					relationType: function() { // 向模态框app中传递参数
						return $scope.record.relationType;
					}
				}
			});
			modalInstance.opened.then(function() { // 模态窗口打开之后执行的函数

			});
			modalInstance.result.then(function(result) {

				if(result[0] != "flagController") { //房东
					$scope.record.houseName = result.selected;
					$scope.record.houseIdArray = result.houseIdArray;
					$scope.record.address = result.houseAddress;
				} else { //租客
					$scope.record['houseCode'] = result[1];
					$scope.record['address'] = result[2];
					$scope.record['objectId'] = result[3];
					$scope.record['houseName'] = result[4];
					$scope.record.houseIdArray = null;
				}
			}, function(reason) {
				console.log(reason);
			});
		};

		//楼栋模态框
		$scope.openBuildingInfo = function() {
			var modalInstance = $modal.open({
				templateUrl: 'personBuilding.html', // script标签中定义的id
				controller: 'personBuildingModalCtrl', // modal对应的Controller
				size: 'lg',
				resolve: {
					relationType: function() { // 向模态框app中传递参数
						return $scope.record.relationType;
					}
				}
			});
			modalInstance.opened.then(function() { // 模态窗口打开之后执行的函数

			});
			modalInstance.result.then(function(result) {
				$scope.record.address = result[0];
				$scope.record['objectId'] = result[1];
			}, function(reason) {
				console.log(reason);
			});
		};

		//人员模态框
		$scope.openPersonInfo = function() {
			var modalInstance = $modal.open({
				templateUrl: 'personInfoList.html', //script标签中定义的id
				controller: 'personInfoModalCtrl', //modal对应的Controller
				size: 'lg',
				resolve: {
					items: function() {
						return $scope.items;
					}
				}
			});

			modalInstance.opened.then(function() { // 模态窗口打开之后执行的函数  

			});
			modalInstance.result.then(function(result) {
				console.log(result[0])
				$scope.record.personName = result[0];
				console.log($scope.record.personName);
				$scope.record.personId = result[1];
				$scope.record.authNo = result[2];
				$scope.record.mobileNo = result[3];
				console.log($('#personName').val());
			}, function(reason) {
				console.log(reason);
			});
		};

		//日期控件
		$scope.dateOptions = {
			startingDay: 1,
			class: 'datepicker'
		};
		//日期控件
		$scope.format = "yyyy-MM-dd";
		$scope.openDatepicker = function($event, i) {
			$event.preventDefault();
			$event.stopPropagation();
			if(i == 1) {
				$scope.startDate = true;
			} else if(i == 2) {
				$scope.endDate = true;
			}
		}
	}
]);

//房屋模态框
app.controller('personHouseModalCtrl', function($scope, $rootScope, $modal,
	$modalInstance, relationType) {

	// 查询条件、参数，用于传递给后台使用
	$scope.param = {};

	// 若为1则显示房东复选框的模态框；若为0则显示租客单选框的模态框。
	$scope.tag = 0;

	$scope.search = function() {
		$scope.loading = true;
		if($scope.pageFlag != 1) { //未点击翻页
			$scope.param.pageNum = 1; //将页码设置为1，即跳转回第一页
		} else { //点击翻页
			$scope.pageFlag = 2; //将开关值设置为2
		}
		$scope.param.houseStatus = "4";
		$.ajax({
			type: 'POST',
			dataType: 'json',
			contentType: 'application/json;charset=UTF-8',
			url: '/dac/bm/house/list',
			data: angular.toJson($scope.param)
		}).then(function(result) {
			$scope.loading = false;
			if(result.httpCode == 200) {
				$scope.pageInfo = result;
				console.log(result);
			} else {
				$scope.msg = result.msg;
			}
			$scope.$apply();
		});
	}
	// 调用查询函数
	$scope.search();

	$scope.selectHouses = function(houseCode, address, houseId, houseName) {
		$scope.params = [];
		$scope.params[0] = "flagController"; //用于区分租客和房东，有flagController则代表着租客，没有则是房东。
		$scope.params[1] = houseCode;
		$scope.params[2] = address;
		$scope.params[3] = houseId;
		$scope.params[4] = houseName;
		// 这里需要将复选框选中的属性取到，然后交给$modalInstance回显到修改页面
		// 修改页面将复选框传来的值再传给后台
		$modalInstance.close($scope.params);
	};

	// 取消
	$scope.cancel = function() {
		$modalInstance.dismiss('cancel');
	};

	// 翻页
	$scope.pagination = function(page) {
		$scope.param.pageNum = page;
		$scope.pageFlag = 1; //将开关值设置为1
		$scope.search();
	};

	// 定义一个数组，用于接收房东复选框的多值
	$scope.selected = [];
	$scope.houseIdArray = [];
	$scope.houseAddress = [];

	$scope.isChecked = function(houseId) {
		return $scope.selected.indexOf(houseId) >= 0;
	};

	$scope.updateSelection = function($event, item) {
		var checkbox = $event.target;
		var checked = checkbox.checked;
		if(checked) {
			$scope.selected.push(item.houseName + "(" + item.houseCode + ")");
			$scope.houseIdArray.push(item.houseId);
			$scope.houseAddress.push(item.address);
		} else {
			var idx = $scope.selected.indexOf(item.houseName + "(" + item.houseCode + ")");
			$scope.selected.splice(idx, 1);

			var houseIdx = $scope.houseIdArray.indexOf(item.houseId);
			$scope.houseIdArray.splice(houseIdx, 1);

			var addressIdx = $scope.houseAddress.indexOf(item.address);
			$scope.houseAddress.splice(addressIdx, 1);
		}
	};

	$scope.saveHouse = function() {
		$scope.records = {};
		$scope.records.selected = $scope.selected;
		$scope.records.houseIdArray = $scope.houseIdArray;
		$scope.records.houseAddress = $scope.houseAddress;
		$modalInstance.close($scope.records);
	} // 定义一个数组，用于接收房东复选框的多值
	$scope.selected = [];
	$scope.houseIdArray = [];
	$scope.houseAddress = [];

	$scope.isChecked = function(houseId) {
		return $scope.selected.indexOf(houseId) >= 0;
	};

	$scope.updateSelection = function($event, item) {
		var checkbox = $event.target;
		var checked = checkbox.checked;
		if(checked) {
			$scope.selected.push(item.houseName + "(" + item.houseCode + ")");
			$scope.houseIdArray.push(item.houseId);
			$scope.houseAddress.push(item.address);
		} else {
			var idx = $scope.selected.indexOf(item.houseName + "(" + item.houseCode + ")");
			$scope.selected.splice(idx, 1);

			var houseIdx = $scope.houseIdArray.indexOf(item.houseId);
			$scope.houseIdArray.splice(houseIdx, 1);

			var addressIdx = $scope.houseAddress.indexOf(item.address);
			$scope.houseAddress.splice(addressIdx, 1);
		}
	};

	$scope.saveHouse = function() {
		$scope.records = {};
		$scope.records.selected = $scope.selected;
		$scope.records.houseIdArray = $scope.houseIdArray;
		$scope.records.houseAddress = $scope.houseAddress;
		$modalInstance.close($scope.records);
	} // 定义一个数组，用于接收房东复选框的多值
	$scope.selected = [];
	$scope.houseIdArray = [];
	$scope.houseAddress = [];

	$scope.isChecked = function(houseId) {
		return $scope.selected.indexOf(houseId) >= 0;
	};

	$scope.updateSelection = function($event, item) {
		var checkbox = $event.target;
		var checked = checkbox.checked;
		if(checked) {
			$scope.selected.push(item.houseName + "(" + item.houseCode + ")");
			$scope.houseIdArray.push(item.houseId);
			$scope.houseAddress.push(item.address);
		} else {
			var idx = $scope.selected.indexOf(item.houseName + "(" + item.houseCode + ")");
			$scope.selected.splice(idx, 1);

			var houseIdx = $scope.houseIdArray.indexOf(item.houseId);
			$scope.houseIdArray.splice(houseIdx, 1);

			var addressIdx = $scope.houseAddress.indexOf(item.address);
			$scope.houseAddress.splice(addressIdx, 1);
		}
	};

	$scope.saveHouse = function() {
		$scope.records = {};
		$scope.records.selected = $scope.selected;
		$scope.records.houseIdArray = $scope.houseIdArray;
		$scope.records.houseAddress = $scope.houseAddress;
		$modalInstance.close($scope.records);
	}

});

//楼栋模态框
app.controller('personBuildingModalCtrl', function($scope, $rootScope, $modal,
	$modalInstance, relationType) {
	// 查询条件、参数，用于传递给后台使用
	$scope.param = {};

	// 若为1则显示房东复选框的模态框；若为0则显示租客单选框的模态框。
	$scope.tag = 0;

	$scope.search = function() {
		$scope.loading = true;
		if($scope.pageFlag != 1) { //未点击翻页
			$scope.param.pageNum = 1; //将页码设置为1，即跳转回第一页
		} else { //点击翻页
			$scope.pageFlag = 2; //将开关值设置为2
		}
		$scope.param.status = "1";
		$.ajax({
			type: 'PUT',
			dataType: 'json',
			contentType: 'application/json;charset=UTF-8',
			url: '/dac/bm/building/list',
			data: angular.toJson($scope.param)
		}).then(function(result) {
			$scope.loading = false;
			if(result.httpCode == 200) {
				$scope.pageInfo = result;
				console.log(result);
			} else {
				$scope.msg = result.msg;
			}
			$scope.$apply();
		});
	}
	// 调用查询函数
	$scope.search();

	$scope.selectHouses = function(houseCode, address, houseId, houseName) {
		$scope.params = [];
		$scope.params[0] = "flagController"; //用于区分租客和房东，有flagController则代表着租客，没有则是房东。
		$scope.params[1] = houseCode;
		$scope.params[2] = address;
		$scope.params[3] = houseId;
		$scope.params[4] = houseName;
		// 这里需要将复选框选中的属性取到，然后交给$modalInstance回显到修改页面
		// 修改页面将复选框传来的值再传给后台
		$modalInstance.close($scope.params);
	};

	$scope.selectBuilding = function(buildingaddress, buildingid, buildingname) {
		$scope.params = [];
		$scope.params[0] = buildingaddress;
		$scope.params[1] = buildingid;
		$scope.params[2] = buildingname;
		// 这里需要将复选框选中的属性取到，然后交给$modalInstance回显到修改页面
		// 修改页面将复选框传来的值再传给后台
		$modalInstance.close($scope.params);
	};

	// 取消
	$scope.cancel = function() {
		$modalInstance.dismiss('cancel');
	};

	// 翻页
	$scope.pagination = function(page) {
		$scope.param.pageNum = page;
		$scope.pageFlag = 1; //将开关值设置为1
		$scope.search();
	};

	// 定义一个数组，用于接收房东复选框的多值
	$scope.selected = [];
	$scope.houseIdArray = [];
	$scope.houseAddress = [];

	$scope.isChecked = function(houseId) {
		return $scope.selected.indexOf(houseId) >= 0;
	};

	$scope.updateSelection = function($event, item) {
		var checkbox = $event.target;
		var checked = checkbox.checked;
		if(checked) {
			$scope.selected.push(item.houseName + "(" + item.houseCode + ")");
			$scope.houseIdArray.push(item.houseId);
			$scope.houseAddress.push(item.address);
		} else {
			var idx = $scope.selected.indexOf(item.houseName + "(" + item.houseCode + ")");
			$scope.selected.splice(idx, 1);

			var houseIdx = $scope.houseIdArray.indexOf(item.houseId);
			$scope.houseIdArray.splice(houseIdx, 1);

			var addressIdx = $scope.houseAddress.indexOf(item.address);
			$scope.houseAddress.splice(addressIdx, 1);
		}
	};

	$scope.saveHouse = function() {
		$scope.records = {};
		$scope.records.selected = $scope.selected;
		$scope.records.houseIdArray = $scope.houseIdArray;
		$scope.records.houseAddress = $scope.houseAddress;
		$modalInstance.close($scope.records);
	} // 定义一个数组，用于接收房东复选框的多值
	$scope.selected = [];
	$scope.houseIdArray = [];
	$scope.houseAddress = [];

	$scope.isChecked = function(houseId) {
		return $scope.selected.indexOf(houseId) >= 0;
	};

	$scope.updateSelection = function($event, item) {
		var checkbox = $event.target;
		var checked = checkbox.checked;
		if(checked) {
			$scope.selected.push(item.houseName + "(" + item.houseCode + ")");
			$scope.houseIdArray.push(item.houseId);
			$scope.houseAddress.push(item.address);
		} else {
			var idx = $scope.selected.indexOf(item.houseName + "(" + item.houseCode + ")");
			$scope.selected.splice(idx, 1);

			var houseIdx = $scope.houseIdArray.indexOf(item.houseId);
			$scope.houseIdArray.splice(houseIdx, 1);

			var addressIdx = $scope.houseAddress.indexOf(item.address);
			$scope.houseAddress.splice(addressIdx, 1);
		}
	};

	$scope.saveHouse = function() {
		$scope.records = {};
		$scope.records.selected = $scope.selected;
		$scope.records.houseIdArray = $scope.houseIdArray;
		$scope.records.houseAddress = $scope.houseAddress;
		$modalInstance.close($scope.records);
	} // 定义一个数组，用于接收房东复选框的多值
	$scope.selected = [];
	$scope.houseIdArray = [];
	$scope.houseAddress = [];

	$scope.isChecked = function(houseId) {
		return $scope.selected.indexOf(houseId) >= 0;
	};

	$scope.updateSelection = function($event, item) {
		var checkbox = $event.target;
		var checked = checkbox.checked;
		if(checked) {
			$scope.selected.push(item.houseName + "(" + item.houseCode + ")");
			$scope.houseIdArray.push(item.houseId);
			$scope.houseAddress.push(item.address);
		} else {
			var idx = $scope.selected.indexOf(item.houseName + "(" + item.houseCode + ")");
			$scope.selected.splice(idx, 1);

			var houseIdx = $scope.houseIdArray.indexOf(item.houseId);
			$scope.houseIdArray.splice(houseIdx, 1);

			var addressIdx = $scope.houseAddress.indexOf(item.address);
			$scope.houseAddress.splice(addressIdx, 1);
		}
	};

	$scope.saveHouse = function() {
		$scope.records = {};
		$scope.records.selected = $scope.selected;
		$scope.records.houseIdArray = $scope.houseIdArray;
		$scope.records.houseAddress = $scope.houseAddress;
		$modalInstance.close($scope.records);
	}

});

//人员模态框
app.controller('personInfoModalCtrl', function($scope, $rootScope, $modal, $modalInstance, items) {

	$scope.paramP = {};
	$scope.searchP = function() {
		$scope.loading = true;
		if($scope.pageFlag != 1) { //未点击翻页
			$scope.paramP.pageNum = 1; //将页码设置为1，即跳转回第一页
		} else { //点击翻页
			$scope.pageFlag = 2; //将开关值设置为2
		}
		$.ajax({
			type: 'PUT',
			dataType: 'json',
			contentType: 'application/json;charset=UTF-8',
			url: '/dac/userManager/list',
			data: angular.toJson($scope.paramP)
		}).then(function(result) {
			$scope.loading = false;
			if(result.httpCode == 200) {
				$scope.pageInfo = result;
			} else {
				$scope.msg = result.msg;
			}
			$scope.$apply();
		});
	}

	$scope.searchP();

	$scope.selectPersons = function(pid, pname, idCard, mobileNo) {
		$scope.params = [];
		$scope.params[0] = pname;
		$scope.params[1] = pid;
		$scope.params[2] = idCard;
		$scope.params[3] = mobileNo;
		$modalInstance.close($scope.params);
	};
	$scope.cancel = function() {
		$modalInstance.dismiss('cancel');

	};

	// 翻页
	$scope.pagination = function(page) {
		$scope.paramP.pageNum = page;
		$scope.pageFlag = 1; //将开关值设置为1
		$scope.searchP();
	};

});